package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setMealMapper;

    /**
     * 分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        //1.设置分页参数，pageHelper分页插件
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());

        //2.调用mapper层，查询分类数据
        // 下一条sql进行分页，自动加入limit关键字分页
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);

        //3.封装PageResult对象并返回
        return new PageResult(page.getTotal(),page.getResult());

    }

    /**
     * 根据类型查询分类
     * @param categoryDTO
     * @return
     */
    @Override
    public List<Category> list(CategoryDTO categoryDTO) {
        List<Category> categories =  categoryMapper.list(categoryDTO);
        return categories;
    }

    /**
     * 修改分类
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        //属性拷贝
        Category category  = new Category();
        BeanUtils.copyProperties(categoryDTO, category);

        //设置修改人、修改时间
        //使用公共字段自动填充
        /*category.setUpdateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());*/

        categoryMapper.update(category);
    }

    /**
     * 启用、禁用分类
     * @param status
     * @param id
     */
    @Override
    public void status(Integer status, Long id) {
        Category category = new Category();
        category.setStatus(status);
        category.setId(id);

        //调用修改分类
        categoryMapper.update(category);
    }

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void insert(CategoryDTO categoryDTO) {
        //新建一个对象，拷贝属性
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);

        //设置创建人、更新人、创建时间、更新时间、状态信息
        //使用公共字段自动填充
        /*category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());*/
        category.setStatus(0);

        categoryMapper.insert(category);
    }


    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    public void delete(long id) {
        //1.根据id查询，判断该id下是否包含菜品产品
        Integer dishMapperCount =  dishMapper.countByCategoryId(id);
        if (dishMapperCount > 0){
            //如果有产品，提示“当前分类关联了菜品,不能删除”
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }


        //2.根据id查询，判断该id下是否包含套餐产品
        Integer setMealMapperCount = setMealMapper.countByCategoryId(id);
        if (setMealMapperCount > 0){
            //如果有产品，提示“当前分类关联了套餐,不能删除”
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //如果没有产品，删除该分类。
        categoryMapper.delete(id);
    }
}
